from rest_framework import serializers
from course.models import Category, Course, Teacher, Activity, ActivityCourse
from drf_haystack.serializers import HaystackSerializer
from .search_indexes import CourseIndex
from django.conf import settings


class CategoryModel(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ["id", "cname"]


class CategorySerializer(serializers.ModelSerializer):
    children = CategoryModel(many=True)

    class Meta:
        model = Category
        fields = '__all__'


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = '__all__'


class courseSerializer(serializers.ModelSerializer):
    teacher = TeacherSerializer()

    class Meta:
        model = Course
        fields = '__all__'


class CourseIndexesSerializer(HaystackSerializer):
    """
    课程索引结果数据序列化器
    """

    class Meta:
        index_classes = [CourseIndex]
        fields = ['id', 'text', 'coursename', 'price', 'cover']


class AcitivitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Activity
        fields = "__all__"


class ActivityCourseSerializer(serializers.ModelSerializer):
    activity = AcitivitySerializer()

    class Meta:
        model = ActivityCourse
        fields = "__all__"
